package com.wm.blog_admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.query.TBlogQuery;
import com.wm.blog_admin.service.TBlogService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.context.AdminBaseContextHandler;
import com.wm.blog_common.domain.TBlogDO;
import com.wm.blog_common.entity.TBlog;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author 半卷流年
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/blog/blog")
public class TBlogController extends AdminBaseController {

    @Autowired
    private TBlogService tBlogService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TBlogDO> get(@PathVariable Long id) {
        return tBlogService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tBlogService.deleteById(id);
    }

    /**
     * 添加
     * @param tBlog
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TBlog tBlog){
        return tBlogService.insert(tBlog);
    }

    /**
     * 修改
     * @param tBlog
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TBlog tBlog){
        return tBlogService.update(tBlog);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TBlogDO>> page(Integer current, Integer size, TBlogQuery query){
        return tBlogService.page(new QueryPage(current,size),query);
    }


    /**
     * excel导出
     * @param request
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response){
        TBlogQuery query = new TBlogQuery();
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        tBlogService.export(query,response);
    }

    /**
     * 博客分类饼图
     */
    @GetMapping("/blogSortPie")
    public Result blogSortPie(HttpServletRequest request){
        Long adminId = AdminBaseContextHandler.getAdminUserId();
        return tBlogService.blogSortPie(adminId);
    }

    /**
     * 博客详情
     */
    @GetMapping("/blogDetail")
    public Result blogDetail(Long id){
        String userName = AdminBaseContextHandler.getAdminUserName();
        log.info("userName:{}",userName);
        return tBlogService.blogDetail(id);
    }


    /**
     * 推荐博客
     */
    @GetMapping("/recommendBlog")
    public Result recommendBlog(Long blogId){
        TBlogQuery query = new TBlogQuery();
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        query.setId(blogId);
        return tBlogService.recommendBlog(query);
    }

}
